<template>

<ion-page :className="{ 'view-container': true, 'default-mode-view': true, 'demobwfdynaactionview': true, 'shop-examine-tamob-wfdyna-action-view': true }">
    
    <app-mob-header>
        <ion-toolbar v-show="titleStatus" class="ionoc-view-header">
            <ion-buttons slot="start">
                <app-mob-button 
                    v-show="isShowBackButton" 
                    ref="backbtn"
                    iconName="chevron-back" 
                    :text="$t('app.button.back')" 
                    @click="closeView" />
            </ion-buttons>
            <app-mob-title class="view-title"><label class="title-label"><app-mob-icon v-if="model.icon" :name="model.icon"></app-mob-icon> <img v-else-if="model.iconcls" :src="model.iconcls" alt=""> {{$t(model.srfCaption)}}</label></app-mob-title>
        </ion-toolbar>

    
    </app-mob-header>

    <ion-content >
                <div class='view-card view-no-caption view-no-toolbar' >
                <div class="content-container">
                <component 
                    :is="activeForm.name"
                    :viewState="viewState"  
                    :viewparams="viewparams" 
                    :context="context" 
                    :autosave="false" 
                    :viewtag="viewtag"
                    :showBusyIndicator="activeForm.showBusyIndicator"
                    :updateAction="activeForm.updateAction"
                    :removeAction="activeForm.removeAction"
                    :loaddraftAction="activeForm.loaddraftAction"
                    :loadAction="activeForm.loadAction"
                    :createAction="activeForm.createAction"
                    :WFSubmitAction="activeForm.WFSubmitAction"
                    :WFStartAction="activeForm.WFStartAction"
                    name="form"
                    ref='form'>
                </component>
                </div>
            </div>
    </ion-content>
    <ion-footer class="view-footer">
        <div class="option-wf-view-btnbox">
  <app-mob-button 
        class="option-btn medium" 
        color="medium"  
        :text="$t('app.button.cancel')"
        @click="onClickCancel" />
  <app-mob-button 
        class="option-btn success" 
        :text="$t('app.button.confirm')"
        @click="onClickOk" />
</div>

    </ion-footer>
</ion-page>
</template>

<script lang='ts'>
import { Vue, Component, Prop, Provide, Emit, Watch } from 'vue-property-decorator';
import { Subject, Subscription } from 'rxjs';
import GlobalUiService from '@/global-ui-service/global-ui-service';
import Shop_examineService from '@/app-core/service/shop-examine/shop-examine-service';

import Shop_examineUIService from '@/ui-service/shop-examine/shop-examine-ui-action';
import { AnimationService } from '@ibiz-core/service/animation-service'

@Component({
    components: {
    },
})
export default class Shop_examineTAMobWFDynaActionViewBase extends Vue {

    /**
     * 全局ui服务
     *
     * @type {GlobalUiService}
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    protected globaluiservice: GlobalUiService = new GlobalUiService();

    /**
     * 实体服务对象
     *
     * @type {Shop_examineService}
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    protected appEntityService: Shop_examineService = new Shop_examineService();

    /**
     * 实体UI服务对象
     *
     * @type Shop_examineUIService
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    public appUIService: Shop_examineUIService = new Shop_examineUIService(this.$store);

    /**
     * 数据变化
     *
     * @param {*} val
     * @returns {*}
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    @Emit() 
    protected viewDatasChange(val: any):any {
        return val;
    }

    /**
     * 视图上下文
     *
     * @type {string}
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    @Prop() protected _context!: string;

    /**
     * 视图参数
     *
     * @type {string}
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    @Prop() protected _viewparams!: string;

    /**
     * 视图默认使用
     *
     * @type {boolean}
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    @Prop({ default: "routerView" }) protected viewDefaultUsage!: string;

	/**
	 * 视图标识
	 *
	 * @type {string}
	 * @memberof Shop_examineTAMobWFDynaActionViewBase
	 */
	protected viewtag: string = 'a3955aec4e3663486e382c50f504baff';

    /**
     * 视图上下文
     *
     * @type {*}
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    protected context: any = {};

    /**
     * 视图参数
     *
     * @type {*}
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    protected viewparams: any = {};

    /**
     * 是否为子视图
     *
     * @type {boolean}
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    @Prop({ default: false }) protected isChildView?: boolean;

    /**
     * 是否为门户嵌入视图
     *
     * @type {boolean}
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    @Prop({ default: false }) protected isPortalView?: boolean;

    /**
     * 标题状态
     *
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    public titleStatus :boolean = true;

    /**
     * 视图导航上下文
     *
     * @protected
     * @type {*}
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    protected navContext: any = {};

    /**
     * 视图导航参数
     *
     * @protected
     * @type {*}
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    protected navParam: any = {};

    /**
     * 视图模型数据
     *
     * @type {*}
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    protected model: any = {
        srfTitle: '入驻审核',
        srfCaption: 'shop_examine.views.tamobwfdynaactionview.caption',
        srfSubCaption: '',
        dataInfo: '',
        viewname:'shop_examine.tamobwfdynaactionview',
        iconcls: '',
        icon: ''
    }

    /**
     * 视图上下文变化
     *
     * @param {string} newVal
     * @param {string} oldVal
     * @memberof  Shop_examineTAMobWFDynaActionViewBase
     */
    @Watch('_context')
    on_context(newVal: string, oldVal: string) {
        const _this: any = this;
        this.parseViewParam();
        if (!Object.is(newVal, oldVal) && _this.engine) {
            this.$nextTick(()=>{_this.engine.load();})
        }
        
    }

    /**
     * 视图参数变化
     *
     * @param {string} newVal
     * @param {string} oldVal
     * @returns
     * @memberof IBizChart
     */
    @Watch('_viewparams',{immediate: true, deep: true})
    on_viewparams(newVal: string, oldVal: string) {
        this.parseViewParam();
        if(this.viewparams && this.viewparams.actionForm){
                this.computeActivedForm(this.viewparams.actionForm);
            }else{
                this.computeActivedForm(null);
            }

    }

    /**
     * 设置工具栏状态
     *
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    public setViewTitleStatus(){
        const thirdPartyName = this.$store.getters.getThirdPartyName();
        if(thirdPartyName){
            this.titleStatus = false;
        }
    }

    /**
     * 容器模型
     *
     * @type {*}
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    protected containerModel: any = {
        view_form: { name: 'form', type: 'FORM' },
        wflinks: [],
    };

    /**
     * 视图状态订阅对象
     *
     * @type {Subject<{action: string, data: any}>}
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    protected viewState: Subject<ViewState> = new Subject();


    /**
     * 是否显示标题
     *
     * @type {string}
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    @Prop({default:true}) protected showTitle?: boolean;


    /**
     * 工具栏模型集合名
     *
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    public toolbarModelList:any = []

    /**
     * 解析视图参数
     *
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    protected parseViewParam(): void {
        const { context, param } = this.$viewTool.formatNavigateViewParam(this, true);
        this.context = { ...context };
        this.viewparams = { ...param }
    }

    /**
     * 是否显示返回按钮
     *
     * @readonly
     * @type {boolean}
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    get isShowBackButton(): boolean {
        // 存在路由，非路由使用，嵌入
        if (this.viewDefaultUsage === "indexView") {
            return false;
        }
        return true;
    }


    /**
     * 引擎初始化
     *
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    protected engineInit(): void {
    }

    /**
     * Vue声明周期
     *
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    protected created() {
        this.afterCreated();
    }

    /**
     * 执行created后的逻辑
     *
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */    
    protected afterCreated(){
        const secondtag = this.$util.createUUID();
        this.$store.commit('viewaction/createdView', { viewtag: this.viewtag, secondtag: secondtag });
        this.viewtag = secondtag;
        this.parseViewParam();
        this.setViewTitleStatus();


    }


    /**
     * 销毁之前
     *
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    protected beforeDestroy() {
        this.$store.commit('viewaction/removeView', this.viewtag);
    }

    /**
     * Vue声明周期
     *
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    public activated() {
        this.thirdPartyInit();
    }



    /**
     * Vue声明周期(组件初始化完毕)
     *
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    protected mounted() {
        this.afterMounted();
    }


    /**
     * 执行mounted后的逻辑
     * 
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    protected afterMounted(){
        const _this: any = this;
        _this.engineInit();
        if (_this.loadModel && _this.loadModel instanceof Function) {
            _this.loadModel();
        }
        this.thirdPartyInit();
this.viewState.next({ tag: "form", action: "autoload", data: {srfkey:this.context.shop_examine} });

    }

    /**
     * 第三方容器初始化
     * 
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    protected  thirdPartyInit(){
        const _this:any = this;  
        if(!this.isChildView){
            this.$viewTool.setViewTitleOfThirdParty(this.$t(this.model.srfCaption) as string);
            this.$viewTool.setThirdPartyEvent(this.closeView);
            this.$viewTool.initRightBtn(_this.righttoolbarModels,_this.righttoolbar_click);
        }
    }

    /**
     * 销毁视图回调
     *
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    protected destroyed(){
        this.afterDestroyed();
    }

    /**
     * 执行destroyed后的逻辑
     * 
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    protected afterDestroyed(){
        if (this.viewDefaultUsage !== "indexView" && Object.keys(localStorage).length > 0) {
            Object.keys(localStorage).forEach((item: string) => {
                if (item.startsWith(this.context.srfsessionid)) {
                    localStorage.removeItem(item);
                }
            });
        }

    }


    /**
     * 第三方关闭视图
     *
     * @param {any[]} args
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    public quitFun() {
        if (!sessionStorage.getItem("firstQuit")) {  // 首次返回时
            // 缓存首次返回的时间
            window.sessionStorage.setItem("firstQuit", new Date().getTime().toString());
            // 提示再按一次退出
            this.$toast("再按一次退出");
            // 两秒后清除缓存（与提示的持续时间一致）
            setTimeout(() => {window.sessionStorage.removeItem("firstQuit")}, 2000);
        } else {
            // 获取首次返回时间
            let firstQuitTime: any = sessionStorage.getItem("firstQuit");
            // 如果时间差小于两秒 直接关闭
            if (new Date().getTime() - firstQuitTime < 2000) {
                this.$viewTool.ThirdPartyClose();
            }
        }
    }
    
    /**
     * 关闭视图
     *
     * @param {any[]} args
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    protected async closeView(args: any[]): Promise<any> {
        if(this.$store.state.searchformStatus){
             this.$store.commit('setSearchformStatus',false);
             return
        }
        if(this.viewDefaultUsage==="indexView" && this.$route.path === '/appindexview'){
            this.quitFun();
            return;
        }
        if (this.viewDefaultUsage === "routerView" ) {
           if(window.history.length == 1 && this.$viewTool.getThirdPartyName()){
                this.quitFun();
            }else{
                this.$store.commit("deletePage", this.$route.fullPath);
                this.$router.go(-1);
           }
        }
        if (this.viewDefaultUsage === "actionView") {
            this.$emit("close", { status: "success", action: "close", data: args instanceof MouseEvent ? null : args });
        }
        
    }

    /**
     * 刷新数据
     *
     * @readonly
     * @type {(number | null)}
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    get refreshdata(): number | null {
        return this.$store.getters['viewaction/getRefreshData'](this.viewtag);
    }

    /**
     * 监控数据变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @returns
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    @Watch('refreshdata')
    onRefreshData(newVal: any, oldVal: any) {
        if (newVal === null || newVal === undefined) {
            return;
        }
        if (newVal === 0) {
            return;
        }
        const _this: any = this;
        if (_this.onRefreshView && _this.onRefreshView instanceof Function) {
            _this.onRefreshView();
        }
    }

    /**
     * 初始化导航栏标题
     *
     * @param {*} val
     * @param {boolean} isCreate
     * @returns
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    public initNavCaption(val:any,isCreate:boolean){
        this.$viewTool.setViewTitleOfThirdParty(this.$t(this.model.srfCaption) as string);        
    }



    /**
     * 当前激活表单
     * 
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    public activeForm:any = {};

    /**
     * 所有表单
     * 
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    /**
     * 所有表单数据
     * 
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    public allForm: any = {
        "form":{name:"view_form",autosave:"false",showBusyIndicator:"true",updateAction:"Update",removeAction:"Remove",loaddraftAction:"GetDraft",loadAction:"Get",createAction:"Create",WFSubmitAction:"",WFStartAction:""}
    };

    /**
     * 计算激活表单
     * 
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    public computeActivedForm(inputForm:any){
        if(!inputForm){
            this.activeForm = this.allForm && Object.values(this.allForm)[0];
            return;
        }
        if(this.allForm && Object.keys(this.allForm).length >0){
            Object.keys(this.allForm).forEach((name:string) =>{
                if(Object.is(name,`wfform_${inputForm.toLowerCase()}`)){
                    this.activeForm = this.allForm[name];
                }
            })
        }
    }

    /**
     * 确认
     * 
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    public onClickOk(){
        let xData:any = this.$refs.form;
        this.$store.commit('viewaction/setViewDataChange', { viewtag: this.viewtag, viewdatachange: false });
        if(xData){
            let preFormData:any = xData.getData();
            let nextFormData:any = xData.transformData(preFormData);
            Object.assign(preFormData,nextFormData);
            this.$store.commit('viewaction/setViewDataChange', { viewtag: this.viewtag, viewdatachange: false });
            this.$emit('viewdataschange', [preFormData]);
            this.$emit('close', null);
        }
    }

    /**
     * 取消
     * 
     * @memberof Shop_examineTAMobWFDynaActionViewBase
     */
    public onClickCancel(){
        this.$emit('close', null);
    }


}
</script>

<style lang='less'>
@import './shop-examine-tamob-wfdyna-action-view.less';
</style>